<?php
class DSF_Content_Template
{
    const PROPERTIES_FILENAME = 'properties.xml';
    const FORM_FILENAME = 'form.php';
    const ALL_TEMPLATES = 'all';
    protected $_folder;
    protected $_template;
    protected $_templatePath;
    public $properties;
    
    public function __construct($folder, $template, $templatePath)
    {
        $this->_folder = $folder;
        $this->_template = $template;
        $this->_templatePath = $templatePath;
        if(!empty($folder) && !empty($template)) {
        	$this->_loadProperties();
        }
    }
    
    public function getAllowedChildTemplates()
    {
    	if(isset($this->_properties)) {
	        if(is_object($this->_properties->allowedChildren)) {
	            $allowedChildren = $this->_properties->allowedChildren->template->toArray();
	            if(is_array($allowedChildren)) {
	                foreach ($allowedChildren as $template) {
	                    $allowedTemplates[$template] = ucwords(str_replace('_',' ', $template));
	                }
	                return $allowedTemplates;
	            }
	        }elseif($this->_properties->allowedChildren === 0 ) {
	            return false;
	        }
        }else{
        	//if no template is passed then you can add any subtemplate
            $loader = new DSF_Content_Template_Loader();
            return $loader->getTemplates();
        }
    }
    
    public function getForm()
    {
    	$pathToForm = $this->_templatePath . '/' . $this->_folder . '/' . $this->_template . '/' . self::FORM_FILENAME ;
    	require_once($pathToForm);
    	$formClass = ucfirst($this->_folder) . '_' . ucfirst($this->_template) . '_Form';
    	return new $formClass();
    }
    
    protected function _loadProperties()
    {
        $path = $this->_templatePath . '/' . $this->_folder . '/' . $this->_template . '/' . self::PROPERTIES_FILENAME;
        $this->_properties = new Zend_Config_Xml(strtolower($path));
    }
     
    
}
